import java.util.Iterator;
import java.util.function.Consumer;

public class SentryLinkList {
    public static void main(String[] args) {
        SentryLlist s = new SentryLlist();
        s.addLast(1);
        s.addLast(2);
        s.addLast(3);
        s.addLast(4);
        s.addLast(5);
        s.addLast(6);

        s.loop3();
    }


}

class SentryLlist implements Iterable<Integer>{

    Node head = new Node(0,null);


    //遍历方法三
    @Override
    public Iterator<Integer> iterator() {
        return new Iterator<Integer>() {
            Node p = head.next;
            @Override
            public boolean hasNext() {
                //判断是否有下一个
                return p!=null;
            }

            @Override
            public Integer next() {
                //返回当前元素，并判断下一位置元素
                int v = p.value;
                p = p.next;
                return v;
            }
        };
    }

    private static class Node{
        int value;
        Node next;



        public  Node(int value,  Node next){
            this.value = value;
            this.next = next;
        }
    }

    public void addFirst(int value){
        insert(0,value);
    }

    //遍历:方法一
    public void loop(){
        Node p = head.next;
        while (p != null){
            System.out.println(p.value);
            p = p.next;
        }
    }

    //遍历:方法二
    public void loop1(Consumer<Integer> consumer){
        for (Node p =head.next;  p != null; p=p.next){
            consumer.accept(p.value);
        }
    }

    //寻找要插入元素位置
    private Node findLast(){
        Node p = head;
//         for (p = head;p.next !=null;){
//             p = p.next;
//         }

        while (p.next!=null){
            p = p.next;
        }
        return p;
    }

    //插入链表尾部
    public void addLast(int value){
        Node prev = findLast();
        prev.next = new Node(value, null);
    }


    //根据索引找元素
    public Node findNode(int index){
        int i = -1;
        for (Node p = head.next;p != null;p = p.next,i++){
            if(i == index){
                return p;
            }
        }
        return null;
    }

    public int get(int index){
        Node p =findNode(index);
        if (p == null){
            return 0;
        }
        return p.value;
    }


    //删除首个
    public void removeFirst(){
        if (head == null){
            return;
        }
        head = head.next;
    }

    //根据索引删除
    public void remove(int index){
        Node p = findNode(index-1);
        if (p==null){
            return;
        }
        Node remove = p.next;
        if (remove==null){
            return;
        }
        p.next = remove.next;
    }

        //根据索引插入元素
    public void insert(int index,int value){
        Node p = findNode(index-1);
        if (p==null){
            return;
        }
        p.next = new Node(value,p.next);
    }

    //递归
    public void loop3(){
        recur(head.next);
    }
    public  void recur(Node cur){
        if (cur==null){
            return;
        }
        System.out.println(cur.value);
        recur(cur.next);
    }



}
